package jdk8;

import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author oldliu
 * @since 1.0
 */
public class BankTest {
    public static void main(String[] args) {
        List<Bank> banks = Arrays.asList(new Bank("1", "liu1", "hefei", Math.random() * 1000), new Bank("2", "liu2",
                "beijing", Math.random() * 1000), new Bank("3", "liu3", "hefei", Math.random() * 1000), new Bank("3",
                "老刘", "anqing", Math.random() * 1000), new Bank("4", "老马", "hefei", Math.random() * 1000), new Bank(
                "5", "小宋", "anqing", Math.random() * 1000), new Bank("6", "老吴", "hefei",
                Math.random() * 1000), new Bank("7", "小张", "huaibei", Math.random() * 1000));
        //1:循环输出账户
        banks.forEach(System.out::println);
        /**
         *
         * 2：统计账户的平均金额
         * 3：排序
         * 4：分类找出每个城市的账号信息，Map<城市，List<账户对象>>
         *      //5：账号余额最大的账户信息
         *       //6:统计各个城市city,账户的人数，Map<String,int>...
         *        //7:统计各个城市city,账户的信息
         */
        //2：统计账户的平均金额
        Double avg = banks.stream().collect
                (Collectors.averagingDouble(x -> x.getMoney()));
        System.out.println("平均账户余额：" + avg);
        //2-1：所有人的账户总金额
        Double all = banks.stream().collect
                (Collectors.summingDouble(x -> x.getMoney()));
        System.out.println("总金额：" + all);
        //banks.sort((x1,x2)->(int)(x1.getMoney()-x2.getMoney()));
        List<Bank> l2 =
                banks.stream().sorted((x1, x2) -> x2.getNo().compareTo(x1.getNo())).collect(Collectors.toList());
        l2.forEach(System.out::println);
        //2-3:扣10块钱年卡费
        banks.forEach(x -> x.setMoney(x.getMoney() - 10));
        System.out.println("扣费后：");
        banks.forEach(System.out::println);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
        //4:分类找出每个城市的账号信息，Map<城市，List<账户对象>>
        System.out.println("每个城市的账号信息:");
        Map<String, List<Bank>> city = banks.stream().collect(Collectors.groupingBy(x -> x.getCity(),
                Collectors.toList()));

//        Map<String,List<Bank>> city=new HashMap<>();
//        for(Bank b:banks){
////            List<Bank> banks1 = city.get(b.getCity());
////            if(banks1==null)
////                banks1=new ArrayList<>();
//            List<Bank> orDefault = city.getOrDefault(b.getCity(), new ArrayList<Bank>());
//            orDefault.add(b);
//            city.put(b.getCity(),orDefault);
//        }
        city.forEach((x, y) -> {
            System.out.println(x + "," + y);
        });
        //各个城市账户数量
        Map<String, Long> city2 = banks.stream().collect(Collectors.groupingBy(x -> x.getCity(),
                Collectors.counting()));
        System.out.println(city2);
        Bank max = Collections.max(banks, (x, y) -> (int) (x.getMoney() - y.getMoney()));
        System.out.println("钱最多的人>:" + max);
        Optional<Bank> collect =
                banks.stream().collect(Collectors.maxBy((x, y) -> (int) (x.getMoney() - y.getMoney())));
        if (collect.isPresent()) {
            System.out.println(collect.get() + "钱最多");
        }
        System.out.println(">>>>>>>>每个城市人余额最多的人:>>>>>>>>>>>");

        Map<String, Optional<Bank>> city3 = banks.stream().collect(Collectors.groupingBy(x -> x.getCity(),
                Collectors.maxBy((x, y) -> (int) (x.getMoney() - y.getMoney()))
        ));
        city3.forEach((x, y) -> {
            System.out.println(x + "--" + y.get());
        });
        System.out.println("每个城市账户余额的平均值：");
        //Map(String,double)
        System.out.println("8统计 账户600元以上（包含）的人数，200（含）-600的人数，200以下的人数\n" +
                "        ");

        Map<String, Long> city4 = banks.stream().collect(Collectors.groupingBy(x -> {
            if (x.getMoney() < 200)
                return "200以下";
            else if (x.getMoney() >= 200 && x.getMoney() < 600)
                return "200-600";
            return "600以上";
        }, Collectors.counting()));
        System.out.println(city4);

        System.out.println("统计结果：");
        DoubleSummaryStatistics stat =
                banks.stream().mapToDouble(x -> x.getMoney()).summaryStatistics();
        System.out.println("最大："+stat.getMax());
        System.out.println("最小："+stat.getMin());
        System.out.println("平均："+stat.getAverage());
        System.out.println("和："+stat.getSum());
    }
}
